package com.sunny.dao.mongo.download;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.DBObject;
import com.sunny.dao.mongo.MongoDao;
import com.sunny.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Sunny on 2016/4/27.
 */
@Service
public class ImageDownload2 {

    @Autowired
    MongoDao mongoDao;

    public final static int per = 20000;
    public final static int  perThread = 20; //每个线程执行多少个2w

    Logger logger = LoggerFactory.getLogger(ImageDownload2.class);


    int totalCount = 0;
    int downErrorCount = 0;//下载失败数量
    int upErrorCount = 0;//上传失败数量
    int successCount = 0;//成功数量
    int creditIdCount = 0;
    int picTotalCount = 0;

    public void downloadEntry(){
      for (int i = 0;i< 1 ;i++){
          logger.info("{}begin ============================",i);
          download(i,i*per);
      }
    }


    private void download(int dirIndex,int skip){
        logger.info("dirIndex={} begin----------------------------------- ",dirIndex);
       /* int downErrorCount =0 ;//下载失败数量
        int successCount =0;*/
        List<DBObject> lists = new ArrayList();
        List<Map<String,String>> mapList = new ArrayList();
        try {
            logger.info("skip={},limit={}",skip,per);
            for(DBObject object:mongoDao.findByFieldsDBCursor(skip,per)){
                lists.add(object);
            }
            logger.info("query data size ={}",lists.size());
            mapList = dealDBObject(lists);
        }catch (Exception e) {
            logger.error("query data exception,dirIndex={},message={}",dirIndex, e.getMessage());
            e.printStackTrace();
            return;
        }
        /**
         * 处理所有的图片信息
         *
         */
        String title = "http://cimg.fenqile.com/";
        for(int j = 0;j<mapList.size();j++){
            Map<String,String> map = mapList.get(j);
            String credit_id = map.get("credit_id").toString();
            Set<Map.Entry<String, String>> entrys = map.entrySet();
            for(Map.Entry<String, String> entry:entrys){
              //  System.out.println("key:"+entry.getKey()+",value:"+entry.getValue());
                //下载图片
                if( !"credit_id".equals(entry.getKey())){
                    String savePath = credit_id+"_"+entry.getKey()+entry.getValue().replaceAll("/","_");
                    String fileUrl = title+entry.getValue();
               //     logger.info(fileUrl);
                 //   logger.info(savePath);
                   // boolean b =

                    saveUrlAs(dirIndex,fileUrl,savePath);
                   /* if(b) downErrorCount++;
                    else successCount ++;*/
                }
            }
        }
        logger.info("totalCount={}",totalCount);
        logger.info("dirIndex={} end ,creditIdCount={},picTotalCount={}, successCount={},downErrorCount={},upErrorCount={} -----------------------------------",dirIndex,creditIdCount,picTotalCount,successCount,downErrorCount,upErrorCount);
    }

    /**
     * 将查询所得数据处理之后存放到map中。
     * @param objectList
     * @return  List<Map<String,String>>
     */
    private List<Map<String,String>> dealDBObject(List<DBObject> objectList ){
        List<Map<String,String>> mapList = new ArrayList<Map<String, String>>();
        for(int i = 0;i<objectList.size();i++){
            DBObject object = objectList.get(i);
            creditIdCount ++;
            Map<String,String> map = new HashMap<String, String>();
            ObjectMapper mapper = new ObjectMapper();
            JsonNode node ;
            try {
                node = mapper.readTree(object.toString());
            } catch (IOException e) {
                logger.error("read DBObject deal list size = {}, exception ={}",i,e.getMessage());
                e.printStackTrace();
                continue;
            }
            String credit_id 		        ;
            String credit_front		        ;
            String personal_pic		        ;
            String credit_back		        ;
            String student_id_pic	        ;
            String fee_list_pic		        ;
            String user_info_pic	        ;
            String dorm_pic			        ;
            String school_roll_pic	        ;
            String contract_front	        ;
            String chis_school_rol_pic      ;
            String intranet_school_rol_pic  ;
            try{
               credit_id 		       =  node.get("credit_id") == null     ?"":node.get("credit_id").toString().replaceAll("\"","");
               credit_front		       =node.get("credit_front") == null    ?"":node.get("credit_front").toString().replaceAll("\"","");
               personal_pic		       =node.get("personal_pic") == null    ?"":node.get("personal_pic").toString().replaceAll("\"","");
               credit_back		       =node.get("credit_back") == null     ?"":node.get("credit_back").toString().replaceAll("\"","");
               student_id_pic	       =node.get("student_id_pic") == null  ?"":node.get("student_id_pic").toString().replaceAll("\"","");
               fee_list_pic		       =node.get("fee_list_pic") == null    ?"":node.get("fee_list_pic").toString().replaceAll("\"","");
               user_info_pic	       =node.get("user_info_pic") == null   ?"":node.get("user_info_pic").toString().replaceAll("\"","");
               dorm_pic			       =node.get("dorm_pic") == null        ? "":node.get("dorm_pic").toString().replaceAll("\"","");
               school_roll_pic	       =node.get("school_roll_pic") == null   ?"":node.get("school_roll_pic").toString().replaceAll("\"","");
               contract_front	       =node.get("contract_front") == null   ?"":node.get("contract_front").toString().replaceAll("\"","");
               chis_school_rol_pic     = node.get("chis_school_rol_pic") == null?"":node.get("chis_school_rol_pic").toString().replaceAll("\"","");
               intranet_school_rol_pic = node.get("intranet_school_rol_pic") == null?"":node.get("intranet_school_rol_pic").toString().replaceAll("\"","");
            }catch (Exception e){
                logger.error("get pic info exception ={}",e.getMessage());
                continue;
            }
            if(StringUtil.isNotEmpty(credit_id)) {
                map.put("credit_id", credit_id);
                picTotalCount ++;
            }
            if(StringUtil.isNotEmpty(credit_front)){
                map.put("credit_front",credit_front);
                picTotalCount ++;
            }
            if(StringUtil.isNotEmpty(personal_pic)){
                map.put("personal_pic",personal_pic);
                picTotalCount ++;
            }
            if(StringUtil.isNotEmpty(credit_back)){
                map.put("credit_back",credit_back	);
                picTotalCount ++;
            }
            if(StringUtil.isNotEmpty(student_id_pic)){
                map.put("student_id_pic",student_id_pic);
                picTotalCount ++;
            }
            if(StringUtil.isNotEmpty(fee_list_pic)){
                map.put("fee_list_pic",fee_list_pic	);
                picTotalCount ++;
            }
            if(StringUtil.isNotEmpty(user_info_pic)){
                map.put("user_info_pic",user_info_pic);
                picTotalCount ++;
            }
            if(StringUtil.isNotEmpty(dorm_pic)){
                map.put("dorm_pic",dorm_pic		);
                picTotalCount ++;
            }
            if(StringUtil.isNotEmpty(school_roll_pic)){
                map.put("school_roll_pic",school_roll_pic);
                picTotalCount ++;
            }
            if(StringUtil.isNotEmpty(contract_front)){
                map.put("contract_front",contract_front);
                picTotalCount ++;
            }
            if(StringUtil.isNotEmpty(chis_school_rol_pic)){
                map.put("chis_school_rol_pic",chis_school_rol_pic);
                picTotalCount ++;
            }
            if(StringUtil.isNotEmpty(intranet_school_rol_pic)){
                map.put("intranet_school_rol_pic",intranet_school_rol_pic);
                picTotalCount ++;
            }
            mapList.add(map);
        }
        logger.info("creditIdCount={},picTotalCount={}",creditIdCount,picTotalCount);
        //总处理
        return mapList;
    }



    public  void saveUrlAs(int dirIndex,String fileUrl,String savePath) {
        URL url = null;
        InputStream is = null;
        OutputStream os  = null;
        try {
            url = new URL(fileUrl);
        } catch (MalformedURLException e) {
            logger.error("url exeption={}",fileUrl);
            downErrorCount ++;
        }
        String fileStr = "D:\\jsonimages\\images\\" + dirIndex;
        File file = new File(fileStr);
        if(!file.exists()){
            logger.info("create direction ={}",fileStr);
            file.mkdir();
        }
        try {
            is =  url.openStream();
        } catch (IOException e) {
            logger.error("download exception,message={}",e.getMessage());
            downErrorCount ++;
        }
        try {
            os = new FileOutputStream(fileStr+"\\"+savePath);
        } catch (FileNotFoundException e) {
            logger.error("upload exception,message={}",e.getMessage());
            upErrorCount ++;
         //   e.printStackTrace();
        }
        byte[] buffer = new byte[4096];
        int count = 0;
        try {
            while ((count = is.read(buffer)) > 0){
                os.write(buffer, 0, count);
            }
            successCount ++;
        } catch (IOException e) {
              // e.printStackTrace();
            logger.error("write exception ,message={}",e.getMessage());
            upErrorCount ++;
        }finally {
            try {
                if(null != os)  os.close();
                if(null != is)  is.close();
            } catch (IOException e) {
                logger.error("close resources exception,message={}",e.getMessage());
            }
        }
    }

}
